<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">任务名称：</div>
          <el-input
            v-model="taskName"
            clearable
            placeholder="请输入任务名称"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">执行人：</div>
          <el-select
            clearable
            v-model="personNo"
            placeholder="请选择"
            @change="queryPageList"
          >
            <el-option
              v-for="item in mixUserList"
              :key="item.personNo"
              :label="item.username"
              :value="item.personNo"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">生成时间：</div>
          <el-date-picker
            v-model="generateTime"
            type="datetime"
            placeholder="选择生成时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="queryPageList()"
          />
        </div>
      </div>
    </template>
    <template v-slot:typenodeslot>
      <SelectButton
        :topitem-select-list="topitemSelectList"
        @topitemtype="topitemtype"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="taskId"
        :table-data="tableData"
        :table-columns="tableColumnsData"
        :showSelectColumn="false"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        @sizeChange="changePageSize"
        :table-column-sum="topitemtypeId"
        :loadSaveColumnWhenTableChange="loadSavedColumns"
        @pageChange="changeCurrentPage"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template slot="equipment" slot-scope="scoped" v-if="scoped.row.taskId">
          <span
            class="table-btns-box"
            @click="mixQueryInspectionPlanTask(scoped.row.taskId)"
          >
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>
        <template
          slot="notifyTemp"
          slot-scope="scoped"
          v-if="scoped.row.taskId"
        >
          <span class="table-btns-box" @click="ShowNotifyDialog(scoped.row)">
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>

        <template slot="operate" slot-scope="scoped" v-if="scoped.row.taskId">
          <span
            v-if="scoped.row.taskId &&topitemtypeId==='COMPLETED'"
            class="table-btns-box"
            @click="ShowTaskResultDialog(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看处理详情</span>
          </span>
          <span
            v-if="scoped.row.taskId &&(topitemtypeId==='PUBLISHED'||topitemtypeId==='RECEIVED'||topitemtypeId==='COMPLETED'||topitemtypeId==='OVERDUE')"
            class="table-btns-box"
            @click="queryDetail(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>
      </e-table>
      <ShowNotifyDialog
        ref="ShowDetailDialog"
        :noticeConfigNo="editObject.noticeConfigNo"
        :show.sync="showNotifyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <SeeEquip
        :is-show-equip="isShowEquip"
        :equip-data="showCheckList"
        @closeDialog="isShowEquip = false"
      />
      <ShowTaskResultDialog
        ref="ShowTaskResultDialog"
        :show.sync="showDetailDialog"
        :taskId="taskId"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import SeeEquip from "@/components/seeEquip/index.vue";
import Pagination from "@/components/Pagination";
import inspectionTaskApi from "@/api/equip-inspection/inspection-task";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import { mymixin } from "@/utils/mixin";
import { maintenanceMixins } from "@/utils/maintenance-mixins";
import SelectButton from "@/components/equipTypeSelectButton/selcetButton.vue";
import ShowNotifyDialog from "./components/show-notify-dialog.vue";
import { mapGetters } from "vuex";
import { getToken } from "@/utils/auth";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
import { deepClone } from "@/utils";
import ShowTaskResultDialog from "@/views/equip-maintenance-manage/components/show-task-result-diaog.vue";
export default {
  name: "InspectionTask",
  components: {
    Pagination,
    BaseMainSlot,
    SelectButton,
    ShowNotifyDialog,
    SeeEquip,
    ShowTaskResultDialog,
  },
  mixins: [mymixin, maintenanceMixins],
  data() {
    return {
      loadSavedColumns: true,
      //  搜索条件:设备名称：
      equipmentName: "",
      equipmentArr: [],
      //  搜索条件:执行人
      personNo: "",
      equipmentList: [], //设施列表
      // 搜索条件:任务模板名称
      planTaskModelName: "",
      // 所搜条件：任务名称
      taskName: "",
      topitemtypeId: "PUBLISHED",
      showCheckDialog: false,
      showDetailDialog: false,
      showNotifyDialog: false,
      generateTime: "",
      taskId: 0,
      status: "",
      overdue: "",
      topitemSelectList: [
        //{ id: "UNAUDIT", name: "待审核" },
        { id: "PUBLISHED", name: "已发布" },
        { id: "RECEIVED", name: "执行中" },
        { id: "COMPLETED", name: "已完成" },
        { id: "OVERDUE", name: "已逾期" },
      ],
      // 表单展示项
      tableLabel: [
        {
          label: "任务编号",
          param: "taskCode",
          show: true,
        },
        {
          label: "任务名称",
          param: "taskName",
          show: true,
          notSearch: true,
        },
        {
          label: "关联设备",
          param: "equipment",
          show: true,
          notSearch: true,
          slotName: "equipment",
          width: 120,
        },
        {
          label: "任务生成时间",
          param: "generateTime", // 表格显示参数
          show: true,
          type: "datetime",
        },
        {
          label: "执行人",
          param: "userName",
          show: true,
          width: 200,
        },
        {
          label: "任务执行时间",
          param: "performStartTime",
          type: "datetime_range",
          show: true,
          width: 400,
          notSearch: true,
        },
        {
          label: "任务通知模版",
          param: "notifyTemp",
          slotName: "notifyTemp",
          show: true,
          notSearch: true,
        },
      ],
      tableColumnsData: [],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            type: "text",
            icon: "el-icon-circle-check",
            methods: "operate",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    //this.queryPageList();
    this.mixQueryInspectionHaveCheckList();
    this.mixQueryUserList();
  },
  mounted() {},
  methods: {
    ShowTaskResultDialog(row) {
      this.showDetailDialog = true;
      this.taskId = row.taskId;
    },
    ShowNotifyDialog(row) {
      console.log("ShowNotifyDialog", row);
      this.editObject = row;
      this.showNotifyDialog = true;
    },
    // 条件查询未报废设备
    queryEquipment() {
      this.equipmentArr = [];
      let params = {};
      equipScrapRecordApi.queryEquipment(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              value: data[i],
              equipmentCode: i,
            });
          }
        }
      });
    },
    querySearch(queryString, cd) {
      var equipmentData = this.equipmentArr;
      let results = queryString
        ? equipmentData.filter(this.createFilter(queryString))
        : equipmentData;
      // 调用 callback 返回建议列表的数据
      cd(results);
    },
    createFilter(queryString) {
      return (equipmentData) => {
        return (
          equipmentData.value
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    // 获取点击的分页index
    topitemtype(e) {
      this.topitemtypeId = e;
      this.tableColumnsData = deepClone(this.tableLabel);
      let index = 5;
      let labelItem = {
        label: "任务执行时间",
        param: "startTime",
        type: "datetime_range",
        show: true,
        width: 400,
      };
      if (this.topitemtypeId === "PUBLISHED") {
        this.status = "PUBLISHED";
        this.overdue = false;
        this.tableColumnsData.splice(index, 1);
      } else if (this.topitemtypeId === "RECEIVED") {
        this.status = "RECEIVED";
        this.overdue = false;
        labelItem.label = "执行开始时间";
        this.$set(this.tableColumnsData, index, labelItem);
      } else if (this.topitemtypeId === "COMPLETED") {
        this.status = "COMPLETED";
        this.overdue = false;
        labelItem.slotName = "performStartTime";
        this.$set(this.tableColumnsData, index, labelItem);
        // this.$set(this.tableLabel[1], "label", "任务完成时间");
      } else if (this.topitemtypeId === "OVERDUE") {
        this.status = null;
        this.overdue = true;
        labelItem.label = "逾期时间";
        labelItem.param = "endTime";
        this.$set(this.tableColumnsData, index, labelItem);
      }
      // 获取表格数据
      this.queryPageList();
    },

    queryPageList(e) {
      this.loading = false;
      var params = {
        page: this.currentPage,
        size: this.pageSize,
      };
      var condition = [
        { paramName: "overdue", value: this.overdue, sign: "equals" },
        {
          paramName: "orgainzationMark",
          value: getToken("organizationNo"),
          sign: "equals",
        },
      ];
      if (this.status) {
        condition.push({
          paramName: "status",
          value: this.status,
          sign: "equals",
        });
      }
      // if ((this.startTime && this.endTime) || e?.time) {
      //   if (this.topitemtypeId === "UNAUDIT") {
      //     condition.push({
      //       paramName: "reviewStartTime",
      //       value: this.startTime || e?.time[0],
      //       sign: "gt",
      //     });
      //     condition.push({
      //       paramName: "reviewEndTime",
      //       value: this.endTime || e?.time[1],
      //       sign: "lt",
      //     });
      //   }
      //   if (this.topitemtypeId === "PUBLISHED") {
      //     condition.push({
      //       paramName: "startTime",
      //       value: this.startTime || e?.time[0],
      //       sign: "gt",
      //     });
      //     condition.push({
      //       paramName: "startTime",
      //       value: this.endTime || e?.time[1],
      //       sign: "lt",
      //     });
      //   }

      //   if (this.topitemtypeId === "RECEIVED") {
      //     condition.push({
      //       paramName: "collectionTime",
      //       value: this.startTime || e?.time[0],
      //       sign: "lt",
      //     });
      //     condition.push({
      //       paramName: "collectionTime",
      //       value: this.endTime || e?.time[1],
      //       sign: "gt",
      //     });
      //   }
      //   if (this.topitemtypeId === "COMPLETED") {
      //     condition.push({
      //       paramName: "realEndTime",
      //       value: this.startTime || e?.time[0],
      //       sign: "gt",
      //     });
      //     condition.push({
      //       paramName: "realEndTime",
      //       value: this.endTime || e?.time[1],
      //       sign: "lt",
      //     });
      //   }
      //   if (this.topitemtypeId === "OVERDUE") {
      //     condition.push({
      //       paramName: "endTime",
      //       value: this.startTime || e?.time[0],
      //       sign: "gt",
      //     });
      //     condition.push({
      //       paramName: "endTime",
      //       value: this.endTime || e?.time[1],
      //       sign: "lt",
      //     });
      //   }
      // }
      if (this.taskName || e?.taskName) {
        condition.push({
          paramName: "taskName",
          value: this.taskName || e?.taskName,
          sign: "like",
        });
      }
      if (this.equipmentName || e?.equipmentName) {
        condition.push({
          paramName: "equipmentName",
          value: this.equipmentName || e?.equipmentName,
          sign: "like",
        });
      }
      if (this.personNo || e?.personNo) {
        condition.push({
          paramName: "personNo",
          value: this.personNo || e?.personNo,
          sign: "equals",
        });
      }
      if (this.generateTime || e?.generateTime) {
        condition.push({
          paramName: "generateTime",
          value: this.generateTime || e?.generateTime,
          sign: "gte",
        });
      }
      if (sessionStorage.farming) {
        condition.push({
          paramName: "farming",
          value: sessionStorage.farming,
          sign: "equals",
        });
      }
      params["condition"] = encodeURIComponent(JSON.stringify(condition));
      inspectionTaskApi.queryPageList(params).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.total;
          this.tableData = data.list;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 表格删除
    delmessage(data) {
      inspectionTaskApi.delList({ taskIdList: [data] }).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 查看详情
    queryDetail(row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.nowChanceStau = "showDetail";
      this.titleText = "查看详情";
      this.showDetailDialog = true;
    },
    // 审核
    check(row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.nowChanceStau = "check";
      this.titleText = "审核";
      this.showCheckDialog = true;
    },
    closeDialog() {
      this.showAddEditDialog = false;
      this.showCheckDialog = false;
      this.showDetailDialog = false;
      this.showNotifyDialog = false;

      if (this.nowChanceStau === "check") {
        this.$refs.tableData.setCurrentRow();
        this.queryPageList();
      }
      this.nowChanceStau = "";
      this.titleText = "";
      this.editObject = {};
    },
    // 取消
    cancel() {
      this.nowChanceStau = "";
      this.titleText = "";
      this.editObject = {};
      this.addActive = true;
      this.delActive = false;
      this.showAddEditDialog = false;
      this.showCheckDialog = false;
      this.showDetailDialog = false;
      this.showNotifyDialog = false;
      this.$refs.tableData.setCurrentRow();
      this.$refs.tableData.clearSelection();
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
</style>
